package com.barradata.common.view


import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.LinearLayout
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import com.barradata.R
import com.barradata.common.base.BaseComponent
import com.barradata.common.rxbinding.clickV
import com.barradata.common.utils.EditTextUtils
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.alert_dialog_fragment.*

/**
 * Created by WuNan-QF on 16/9/3.
 */
class AlertDialogFragment : DialogFragment(), BaseComponent {
    override fun compositeDisposable() = CompositeDisposable()

    internal var mTYPE: TYPE = TYPE.DOUBLE_BUTTON
    internal var ensureButtonAction: (()->Unit)? = null
    internal var cancelButtonAction: (()->Unit)? = null
    internal var singleButtonAction: (()->Unit)? = null

    internal var titleStringResId: Int = 0
    internal var titleString: String? = null
    internal var contentString: String? = null
    internal var ensureButtonString: String? = null
    internal var cancelButtonString: String? = null
    internal var singleButtonString: String? = null


    fun setTitle(title: String): AlertDialogFragment {
        titleString = title
        return this
    }

    fun setTitle(@StringRes resId: Int): AlertDialogFragment {
        titleStringResId = resId
        return this
    }

    fun setContent(content: String): AlertDialogFragment {
        contentString = content
        return this
    }

    fun setEnsureButtonText(text: String): AlertDialogFragment {
        ensureButtonString = text
        return this
    }

    fun setCancelButtonText(text: String): AlertDialogFragment {
        cancelButtonString = text
        return this
    }

    fun setSingleButtonText(text: String): AlertDialogFragment {
        singleButtonString = text
        return this
    }

    /**
     * @param cancelable 点击空白区域,dismiss , 默认为 false
     */
    fun setCancelAble(cancelable: Boolean): AlertDialogFragment {
        isCancelable = cancelable
        return this
    }

    fun show(context: AppCompatActivity) {
        EditTextUtils.hideKeyboard(context);
        super.show(context.supportFragmentManager, "AlertDialog")
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val window = dialog.window
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window.requestFeature(Window.FEATURE_NO_TITLE)
        setCancelAble(isCancelable)
        val view = inflater.inflate(R.layout.alert_dialog_fragment, container)
        val view1: View = view.findViewById(R.id.view)
        val linearParams = view1.layoutParams as LinearLayout.LayoutParams
        val m = activity!!.windowManager
        val d = m.defaultDisplay // 获取屏幕宽高用
        //linearParams.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.6
        linearParams.width = (d.width * 0.8).toInt() // 宽度设置为屏幕的0.65
        view1.setLayoutParams(linearParams)
        return view
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (titleStringResId != 0) {
            title_text.text = getString(titleStringResId)
        } else if (titleString.isNullOrEmpty()) {
            title_text.visibility = View.GONE
        } else {
            title_text.visibility = View.VISIBLE
            title_text.text = titleString
        }
        if (contentString.isNullOrEmpty()) {
            content_text.visibility = View.GONE
        } else {
            content_text.visibility = View.VISIBLE
            content_text.text = contentString
        }

        if (ensureButtonString != null) {
            ensure_button_text.text = ensureButtonString
        }
        if (cancelButtonString != null) {
            ensure_button_text.text = cancelButtonString
        }
        if (singleButtonString != null) {
            single_button_text.text = singleButtonString
        }

        if (mTYPE == TYPE.SINGLE_BUTTON) {
            double_button_layout.visibility = View.GONE
            single_button.visibility = View.VISIBLE
            single_button.clickV().subscribe {
                dismiss()
                if (singleButtonAction != null) {
                    singleButtonAction!!.invoke()
                }
            }.autoClean()
        } else if (mTYPE == TYPE.DOUBLE_BUTTON) {
            double_button_layout.visibility = View.VISIBLE
            single_button.visibility = View.GONE
            ensure_button.clickV().subscribe {
                dismiss()
                ensureButtonAction?.invoke()
            }.autoClean()

            cancel_button.clickV().subscribe {
                dismiss()
                cancelButtonAction?.invoke()
            }.autoClean()
        }

    }


    override fun onDestroy() {
        disposeExt()
        super.onDestroy()
    }

    enum class TYPE {
        SINGLE_BUTTON,
        DOUBLE_BUTTON
    }

    companion object {

        fun create(ensureButtonAction: (()->Unit)?, cancelButtonAction: (()->Unit)?): AlertDialogFragment {
            val alertDialogFragment = AlertDialogFragment()
            alertDialogFragment.mTYPE = TYPE.DOUBLE_BUTTON
            alertDialogFragment.ensureButtonAction = ensureButtonAction
            alertDialogFragment.cancelButtonAction = cancelButtonAction
            return alertDialogFragment
        }

        fun create(singleButtonAction: (()->Unit)?): AlertDialogFragment {
            val alertDialogFragment = AlertDialogFragment()
            alertDialogFragment.mTYPE = TYPE.SINGLE_BUTTON
            alertDialogFragment.singleButtonAction = singleButtonAction
            return alertDialogFragment
        }
    }


}
